   		// 扩展API加载完毕后调用onPlusReady回调函数 
document.addEventListener("plusready", plusReady, false);
   		// 扩展API加载完毕，现在可以正常调用扩展API 
		
function plusReady() {
    cmr = plus.camera.getCamera(); // 全局变量
    console.log("API插件加载完毕！");
    
    // 初始化页面，默认激活拍照tab
    document.getElementById('tab-capture').classList.add('active');
}

// 全局变量
let currentGroup = '房屋拍照';
let currentTab = 'capture';

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    console.log('=== 应用初始化开始 ===');
    console.log('DOM加载完成，开始初始化应用');
    
    // 检查关键DOM元素
    console.log('关键DOM元素检查:', {
        'capture-section存在': !!document.getElementById('capture-section'),
        'classify-section存在': !!document.getElementById('classify-section'),
        'gallery-section存在': !!document.getElementById('gallery-section'),
        'tab-capture存在': !!document.getElementById('tab-capture'),
        'tab-gallery存在': !!document.getElementById('tab-gallery'),
        'tab-classify存在': !!document.getElementById('tab-classify')
    });
    
    // 初始化标签页
    console.log('初始化默认标签页: capture');
    switchTab('capture');
    
    // 初始化相册
    console.log('调用相册初始化');
    initGallery();
    
    console.log('=== 应用初始化完成 ===');
});

// 标签页切换函数
function switchTab(tabName) {
    console.log('=== 标签页切换开始 ===');
    console.log('切换到标签页:', tabName);
    console.log('当前标签页:', currentTab);
    
    // 检查DOM元素
    const captureSection = document.getElementById('capture-section');
    const classifySection = document.getElementById('classify-section');
    const gallerySection = document.getElementById('gallery-section');
    const statsSection = document.getElementById('stats-section');
    const tabCapture = document.getElementById('tab-capture');
    const tabGallery = document.getElementById('tab-gallery');
    const tabClassify = document.getElementById('tab-classify');
    const tabStats = document.getElementById('tab-stats');
    
    console.log('DOM元素检查:', {
        'capture-section存在': !!captureSection,
        'classify-section存在': !!classifySection,
        'gallery-section存在': !!gallerySection,
        'stats-section存在': !!statsSection,
        'tab-capture存在': !!tabCapture,
        'tab-gallery存在': !!tabGallery,
        'tab-classify存在': !!tabClassify,
        'tab-stats存在': !!tabStats
    });
    
    // 隐藏所有内容区域
    if (captureSection) captureSection.style.display = 'none';
    if (classifySection) classifySection.style.display = 'none';
    if (gallerySection) gallerySection.style.display = 'none';
    if (statsSection) statsSection.style.display = 'none';
    
    // 移除所有标签页的激活状态
    if (tabCapture) tabCapture.classList.remove('active');
    if (tabGallery) tabGallery.classList.remove('active');
    if (tabClassify) tabClassify.classList.remove('active');
    if (tabStats) tabStats.classList.remove('active');
    
    // 根据标签页名称切换
    switch (tabName) {
        case 'capture':
            console.log('切换到拍照标签页');
            if (captureSection) {
                captureSection.style.display = 'block';
                console.log('capture-section显示成功');
            } else {
                console.error('capture-section元素不存在！');
            }
            if (tabCapture) {
                tabCapture.classList.add('active');
                console.log('tab-capture激活成功');
            } else {
                console.error('tab-capture元素不存在！');
            }
            currentTab = 'capture';
            break;
            
        case 'gallery':
            console.log('切换到相册标签页');
            if (gallerySection) {
                gallerySection.style.display = 'block';
                console.log('gallery-section显示成功');
            } else {
                console.error('gallery-section元素不存在！');
            }
            if (tabGallery) {
                tabGallery.classList.add('active');
                console.log('tab-gallery激活成功');
            } else {
                console.error('tab-gallery元素不存在！');
            }
            
            // 准备刷新相册数据
            console.log('准备刷新相册数据');
            if (typeof refreshGallery === 'function') {
                console.log('调用refreshGallery函数');
                refreshGallery();
            } else {
                console.error('refreshGallery函数不存在');
            }
            currentTab = 'gallery';
            break;
            
        case 'classify':
            console.log('切换到分组标签页');
            if (classifySection) {
                classifySection.style.display = 'block';
                console.log('classify-section显示成功');
            } else {
                console.error('classify-section元素不存在！');
            }
            if (tabClassify) {
                tabClassify.classList.add('active');
                console.log('tab-classify激活成功');
            } else {
                console.error('tab-classify元素不存在！');
            }
            currentTab = 'classify';
            break;
            
        case 'stats':
            console.log('切换到统计标签页');
            if (statsSection) {
                statsSection.style.display = 'block';
                console.log('stats-section显示成功');
            } else {
                console.error('stats-section元素不存在！');
            }
            if (tabStats) {
                tabStats.classList.add('active');
                console.log('tab-stats激活成功');
            } else {
                console.error('tab-stats元素不存在！');
            }
            
            // 准备刷新统计数据
            console.log('准备刷新统计数据');
            if (typeof refreshStats === 'function') {
                console.log('调用refreshStats函数');
                refreshStats();
            } else {
                console.error('refreshStats函数不存在');
            }
            currentTab = 'stats';
            break;
            
        default:
            console.error('未知的标签页:', tabName);
            return;
    }
    
    console.log('当前标签页:', currentTab);
    console.log('=== 标签页切换完成 ===');
}

// 显示提示信息
function showToast(message, type = 'info') {
    console.log('=== 显示Toast提示 ===');
    console.log('提示内容:', message);
    console.log('提示类型:', type);
    
    // 创建提示元素
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.textContent = message;
    
    console.log('Toast元素创建成功:', {
        'className': toast.className,
        'textContent': toast.textContent
    });
    
    // 添加到页面
    document.body.appendChild(toast);
    console.log('Toast元素已添加到页面');
    
    // 显示动画
    setTimeout(() => {
        toast.classList.add('show');
        console.log('Toast显示动画开始');
    }, 100);
    
    // 自动隐藏
    setTimeout(() => {
        toast.classList.remove('show');
        console.log('Toast隐藏动画开始');
        setTimeout(() => {
            if (document.body.contains(toast)) {
                document.body.removeChild(toast);
                console.log('Toast元素已从页面移除');
            }
        }, 300);
    }, 3000);
}

// 更新当前分组显示
function updateGroupDisplay(groupName) {
    console.log('=== 更新分组显示 ===');
    console.log('新分组名称:', groupName);
    console.log('旧分组名称:', currentGroup);
    
    currentGroup = groupName;
    const demoElement = document.getElementById('demo');
    
    console.log('demo元素检查:', {
        '元素存在': !!demoElement,
        '元素类型': demoElement ? demoElement.constructor.name : 'null'
    });
    
    if (demoElement) {
        demoElement.textContent = groupName;
        console.log('分组显示更新成功');
    } else {
        console.error('demo元素不存在！');
    }
}

// 初始化相册功能（已移至gallery.js模块）
function initGallery() {
    console.log('=== 初始化相册功能 ===');
    console.log('相册功能已移至gallery.js模块');
    
    // 检查gallery对象是否存在
    console.log('gallery对象检查:', {
        'gallery存在': typeof gallery !== 'undefined',
        'gallery类型': typeof gallery !== 'undefined' ? gallery.constructor.name : 'undefined'
    });
    
    if (typeof gallery !== 'undefined') {
        console.log('gallery对象存在，调用init方法');
        gallery.init();
    } else {
        console.error('gallery对象不存在！');
				}
			}
